<template>
	<view class="order_deta">
		<Navbar>订单详情</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<view class="status" v-if="pay_status==0">
					<view class="icon">待支付</view>
					<view class="tips">倒计时 {{date}}</view>
				</view>
				<view class="status" v-else-if="pay_status==1">
					<view class="icon success_tips">转入成功</view>
					<view class="tips">已转入您的个人中心,请查看</view>
				</view>
				<view class="status" v-else>
					<view class="icon fail">订单超时</view>
				</view>
				<view class="banner">
					<view class="cover_box">
						<view class="cover">
							<image :src="cover"></image>
						</view>
						<view class="view_box">
							<view class="name">{{name}}</view>
							<view class="number" v-if="number">#{{number}}</view>
						</view>
					</view>
					<view class="sub_title">
						<image :src="`${baseImageUrl}/home/xs.png`"></image>
						<text>订单详情</text>
					</view>
					<view class="one">
						<view class="item">订单金额：￥{{price}}</view>
						<view class="item">订单编号：{{order_no}}</view>
					</view>
					<view class="one two">
						<!-- <view class="item">支付成功，藏品转入中</view> -->
						<view class="item">下单时间（{{created_at}})</view>
					</view>
					<view class="btn_view" v-if="pay_status==0">
						<view class="cancel" @click="cancel">取消订单</view>
						<view class="confirm" @click="show=true">去支付</view>
					</view>
					<view class="btn_view" v-else-if="pay_status==1">
						<view class="cancel" @click="redirectRouter(0)">继续购买</view>
						<view class="confirm" @click="redirectRouter(0)">返回大厅</view>
					</view>
					<view class="btn_view" v-else>
						<view class="cancel" @click="redirectRouter(3)">个人中心</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<u-popup :show="show" mode="bottom" :round="10">
			<PayType :list="pay_mode" :id="id" @close="show=false" @before="show_footer=false" @success="getDeta" />
		</u-popup>
	</view>
</template>

<script>
	import {
		orderInfo,
		orderCancel
	} from '@/request/api.js';
	import PayType from "@/components/common/PayType.vue";
	export default {
		components: {
			PayType
		},
		data() {
			return {
				id: null,

				cover: "",
				name: "",
				number: "",
				price: "",
				order_no: "",
				created_at: "",

				pay_status: -1,
				end_pay_time: "",
				date: "",

				show: false,
				pay_mode: {},
			}
		},
		onLoad(option) {
			this.id = option.id;
		},
		onShow() {
			this.getDeta();
		},
		onUnload() {
			clearTimeout(this.timer);
		},
		onHide() {
			clearTimeout(this.timer);
		},
		methods: {
			async getDeta() {
				clearTimeout(this.timer);
				let {
					info,
					pay_mode
				} = await orderInfo({
					id: this.id
				});

				this.cover = info.product.cover;
				this.name = info.product.name;
				this.number = info.product_number;
				this.price = info.price;
				this.order_no = info.order_no;
				this.created_at = info.created_at;

				this.pay_mode = pay_mode;

				this.pay_status = info.pay_status;
				this.end_pay_time = info.end_pay_time;
				if (this.pay_status == 0) {
					this.handerTime();
				}
				this.show = false;
			},
			handerTime() {
				let date = this.end_pay_time;
				if (date == 0) return;
				let current = parseInt(new Date().getTime() / 1000);
				if (current >= date) {
					this.date = "00:00:00";
					clearTimeout(this.timer);
					this.timer = setTimeout(this.getDeta, 5 * 1000);
				} else {
					let surplus = date - current;
					let hour = parseInt(surplus / 60 / 60 % 24);
					hour = hour < 10 ? '0' + hour : hour;

					let mm = parseInt(surplus / 60 % 60);
					mm = mm < 10 ? '0' + mm : mm;

					let ss = parseInt(surplus % 60);
					ss = ss < 10 ? '0' + ss : ss;
					this.date = `${hour}:${mm}:${ss}`;

					this.timer = setTimeout(this.handerTime, 1000);
				};
			},
			async cancel() {
				await orderCancel({
					id: this.id
				});
				this.$u.toast("取消成功");
				setTimeout(() => {
					this.getDeta();
				}, 1000);
			}
		}
	}
</script>

<style scoped lang="scss">
	.order_deta {
		height: 100vh;
		background: #29170D;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});

			.status {
				padding-bottom: 48rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.icon {
					width: 240rpx;
					height: 240rpx;
					color: #38880F;
					font-size: 40rpx;
					font-weight: 400;
					font-family: LiSu;
					display: flex;
					justify-content: center;
					align-items: flex-end;
					background: url(#{$baseImageUrl}/common/time2.png) no-repeat;
					background-size: 100% 100%;

					&.fail {
						color: #CA372E;
						background: url(#{$baseImageUrl}/common/unlock_fail.png) no-repeat;
						background-size: 100% 100%;
					}

					&.success_tips {
						background: url(#{$baseImageUrl}/common/success.png) no-repeat;
						background-size: 100% 100%;
					}
				}

				.tips {
					text-align: center;
					color: #FFFFFF;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
					margin-top: 20rpx;
				}

				.success_tips {
					width: 240rpx;
					height: 240rpx;
					color: #38880F;
					font-size: 40rpx;
					font-weight: 400;
					font-family: LiSu;
					margin: 0 auto;
					background: url(#{$baseImageUrl}/common/success.png) no-repeat;
					background-size: 100% 100%;
					display: flex;
					justify-content: center;
					align-items: flex-end;
				}

				.tips {
					text-align: center;
					color: #FFFFFF;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
				}
			}

			.banner {
				width: 100%;
				height: 800rpx;
				padding: 32rpx 75rpx 80rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/750_800.png) no-repeat;
				background-size: 100% 100%;

				.cover_box {
					display: flex;

					.cover {
						width: 200rpx;
						height: 200rpx;
						border-radius: 10rpx;
						padding: 4rpx;
						box-sizing: border-box;
						background: #C0B5B6;
						overflow: hidden;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.view_box {
						flex: 1;
						padding: 20rpx 30rpx;
						box-sizing: border-box;
						overflow: hidden;

						.name {
							color: #494338;
							font-size: 50rpx;
							font-weight: 400;
							font-family: LiSu;
						}

						.number {
							color: #494338;
							font-weight: 400;
							font-size: 40rpx;
							font-family: LiSu;
							margin-top: 40rpx;
						}
					}
				}
			}

			.sub_title {
				margin: 40rpx 0;
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}

				text {
					line-height: 32rpx;
					color: #494338;
					font-size: 32rpx;
					font-weight: 400;
					font-family: SimSun;
					padding-left: 12rpx;
					box-sizing: border-box;
				}
			}

			.one {
				border-bottom: 1px solid rgba(73, 67, 56, .2);
				padding-bottom: 10rpx;

				.item {
					color: #494338;
					font-size: 32rpx;
					font-weight: 400;
					font-family: SimSun;
					margin-bottom: 20rpx;
				}

				&.two {
					padding-top: 30rpx;
					border: none;
				}
			}

			.btn_view {
				width: 100%;
				margin-top: 50rpx;
				display: flex;
				justify-content: center;

				view {
					width: 210rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					font-weight: 400;
					font-family: SimSun;
					margin: 0 35rpx;
				}

				.cancel {
					background: url(#{$baseImageUrl}/common/btn3.png) no-repeat;
					background-size: 100% 100%;
				}

				.confirm {
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>